﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace WXR2BlogML
{
	/// <summary>
	/// wordpress still have some problems and known export bugs that will be fixed in the 3.0 milestone
	/// 
	/// quick and dirty fixes
	/// </summary>
	public static class WordpressExportFix
	{
		public static string SanitizeTagSlug(this string text)
		{
			return text.Replace("-", " ");
		}

		/// <summary>
		/// look inside post contents to find any reference to Images and attachment files.
		/// change their address to match the Dexter way of storing data that is from:
		/// WP: http://www.primordialcode.com/wp-content/uploads/CastleWindsorTransientObjectsandReleaseP_EBF6/IoCRelease1.jpg
		/// Dexter: /UserFiles/AUTHORNAME/IoCRelease1.jpg
		/// 
		/// beware, wordpress adds thumbs appending '_thumb' to the file name
		/// 
		/// regex to identify wordpress uploads: "http://.*wp-content/uploads[^"]*"+?
		/// </summary>
		/// <param name="text"></param>
		/// <param name="authorUserName"></param>
		/// <returns></returns>
		public static string ConvertResourceAddresses(string text, string authorUserName)
		{
			const string wordpressPattern = "\"http://[^\"]*?wp-content/uploads[^\"]*?\"";
			string dexterPath = "/UserFiles/" + authorUserName;
			const string separator = "wp-content/uploads";
					
			foreach (Match match in Regex.Matches(text, wordpressPattern))
			{
				if (match.Success)
				{
					string relativePath = match.Value.Substring(match.Value.IndexOf(separator) + separator.Length);
					text = text.Replace(match.Value, "\"" + dexterPath + relativePath);
				}
			} 
			return text;
		}

		/// <summary>
		/// wraps the wp:meta_value content in CDATA sections if missing
		/// </summary>
		/// <param name="text"></param>
		/// <returns></returns>
		public static string WpMetaValueCdataWrapper(string text)
		{
			// look if we have to apply the fix
			const string metaValueCdataOpen = "<wp:meta_value><![CDATA[";
			const string metaValueCDataClose = "]]></wp:meta_value>";
			const string metaValueOpen = "<wp:meta_value>";
			const string metaValueClose = "</wp:meta_value>";
			if (!text.Contains(metaValueCdataOpen))
			{
				// apply the fix
				text = text.Replace(metaValueOpen, metaValueCdataOpen);
				text = text.Replace(metaValueClose, metaValueCDataClose);
			}
			// no need to apply the fix return the value
			return text;
		}

		public static string ApplyFixes(string data)
		{
			XmlSanitizedString text = new XmlSanitizedString(data);
			return WpMetaValueCdataWrapper(text.ToString());
		}

		public static void FixExportFile(string srcfile, string destfile)
		{
			string data;
			using (StreamReader sr = new StreamReader(srcfile))
				data = sr.ReadToEnd();
			string converted = ApplyFixes(data);
			using (StreamWriter sw = new StreamWriter(destfile))
				sw.Write(converted);
		}

	}
}
